﻿using UnityEngine;
using System.Collections;
using System;
using System.Collections.Generic;
namespace SongeNS
{
    public class EventManager : MonoBehaviour
    {

        public static EventManager instance;

        Dictionary<object, Action<string, object>> callBackDic = new Dictionary<object, Action<string, object>>();

        void Awake()
        {
            instance = this;
            DontDestroyOnLoad(gameObject);
        }

        //注册消息
        public void Register(object dispatcher, Action<string, object> callback)
        {
            if (dispatcher == null)
                dispatcher = this;
            if (!callBackDic.ContainsKey(dispatcher))
                callBackDic.Add(dispatcher, callback);
            else
                callBackDic[dispatcher] += callback;
        }

        //传递消息
        public void DispatchEvent(object dispatcher, string eventName, object param = null)
        {
            if (dispatcher == null)
                dispatcher = this;
            if (callBackDic.ContainsKey(dispatcher))
            {
                callBackDic[dispatcher](eventName, param);
            }
        }

        //public void DispatchEvent(object dispatcher, string eventName,params object[] param )
        //{
        //    if (dispatcher == null)
        //        dispatcher = this;
        //    if (callBackDic.ContainsKey(dispatcher))
        //    {
        //        callBackDic[dispatcher](eventName, param);
        //    }
        //}
    }

    public class SEvent<T1>
    {
        public T1 Param1;
        public SEvent(T1 param1)
        {
            Param1 = param1;
        }
    }
    public class SEvent<T1, T2>
    {
        public T1 Param1;
        public T2 Param2;
        public SEvent(T1 param1, T2 param2)
        {
            Param1 = param1;
            Param2 = param2;
        }
    }
    public class SEvent<T1, T2, T3>
    {
        public T1 Param1;
        public T2 Param2;
        public T3 Param3;
        public SEvent(T1 param1, T2 param2, T3 param3)
        {
            Param1 = param1;
            Param2 = param2;
            Param3 = param3;
        }
    }
    public class SEvent<T1, T2, T3, T4>
    {
        public T1 Param1;
        public T2 Param2;
        public T3 Param3;
        public T4 Param4;
        public SEvent(T1 param1, T2 param2, T3 param3, T4 param4)
        {
            Param1 = param1;
            Param2 = param2;
            Param3 = param3;
            Param4 = param4;
        }
    }


}